import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import json

class Testservice:

    @pytest.mark.parametrize('serviceinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/service/1_get_service_list.yaml'))
    def test_get_service_list(self, serviceinfo, get_env_info):
        # 获取service列表接口
        baseUrl = get_env_info
        url = baseUrl + serviceinfo['request']['url'].replace('{namespace}','default')
        method = serviceinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == serviceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('serviceinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/service/2_create_service.yaml'))
    def test_create_service(self, serviceinfo, get_env_info):
        # 创建service接口
        baseUrl = get_env_info
        url = baseUrl + serviceinfo['request']['url'].replace('{namespace}','default')
        method = serviceinfo['request']['method']
        data = serviceinfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        if '-' not in serviceinfo['name']:
            servicename=req.json()['metadata']['name']
            globals()['servicename']=servicename
        assert req.status_code == serviceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('serviceinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/service/3_get_specific_service.yaml'))
    def test_get_specific_service(self, serviceinfo, get_env_info):
        # 获取指定service接口
        baseUrl = get_env_info
        url = baseUrl + serviceinfo['request']['url'].replace('{namespace}','default').replace('{servicename}',globals()['servicename'])
        method = serviceinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '-' not in serviceinfo['name']:
            updatereqbody=req.json()
            globals()['updatereqbody']=updatereqbody
        assert req.status_code == serviceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('serviceinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/service/4_update_service.yaml'))
    def test_update_service(self, serviceinfo, get_env_info):
        # 更新指定service接口
        baseUrl = get_env_info
        url = baseUrl + serviceinfo['request']['url'].replace('{namespace}','default').replace('{servicename}',globals()['servicename'])
        method = serviceinfo['request']['method']
        serviceinfo['request']['data'] = globals()['updatereqbody']
        data = serviceinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == serviceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('serviceinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/service/5_update_section_service.yaml'))
    def test_update_section_service(self, serviceinfo, get_env_info):
        # 部分更新指定service的接口
        baseUrl = get_env_info
        url = baseUrl + serviceinfo['request']['url'].replace('{namespace}','default').replace('{servicename}',globals()['servicename'])
        method = serviceinfo['request']['method']
        data = serviceinfo['request']['data']
        header = serviceinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header,json=data)
        assert req.status_code == serviceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('serviceinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/service/6_delete_specific_service.yaml'))
    def test_delete_specific_service(self, serviceinfo, get_env_info):
        # 删除service接口
        baseUrl = get_env_info
        url = baseUrl + serviceinfo['request']['url'].replace('{namespace}','default').replace('{servicename}',globals()['servicename'])
        method = serviceinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == serviceinfo['validate'][0]['equals']['status_code']